package com.acg.auth.service.impl;

import com.acg.auth.mapper.SysRoleMapper;
import com.acg.auth.pojo.SysUser;
import com.acg.auth.mapper.SysUserMapper;
import com.acg.auth.service.SysMenuService;
import com.acg.auth.service.SysUserService;
import com.acg.auth.vo.RouterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.PortResolverImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author acg
 * @since 2025-08-07
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysRoleMapper roleMapper;


    @Override
    public boolean updateState(String id, Integer status) {
        // 更新条件
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", status).eq("id", id);
        // 进行更新
        boolean update = this.update(updateWrapper);

        return update;
    }

    @Override
    public SysUser getUserByUsername(String username) {
        // 构造查找条件
        LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper<>();
        qw.eq(SysUser::getUsername, username);

        // 进行查找
        SysUser user = this.getOne(qw);
        if (user == null) {
            return null;
        }
        return user;
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        // map 头像、用户名、权限信息 、按钮权限信息
        Map<String, Object> map = new HashMap<>();
        // 根据用户名获取用户信息
        SysUser userByUsername = this.getUserByUsername(username);
        // 放置头像、用户名
        map.put("name",userByUsername.getUsername());
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");

        Long id = userByUsername.getId();
        System.err.println("id"+id);


        //根据用户id获取用户按钮权限
        List<String> permsList = menuService.findUserPermsList(userByUsername.getId());

        //根据用户id获取菜单权限值
        List<RouterVo> routerVoList =menuService.findUserMenuList(id);

        //根据用户id获取角色名
        List<String> roleName = roleMapper.selectRoleNameByUserId(id);

        map.put("roleName",roleName);


        map.put("roles",routerVoList);

        map.put("id",id);

        System.out.println(permsList);
        System.out.println(routerVoList);

        map.put("buttons", permsList);
        map.put("routers", routerVoList);


        return map;
    }
}
